package cn.songhaiqing.walle.service;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.greenrobot.eventbus.EventBus;

import cn.songhaiqing.walle.Event.BloodEvent;
import cn.songhaiqing.walle.bean.MyApp;
import cn.songhaiqing.walle.bledemo.MainActivity;
import cn.songhaiqing.walle.bledemo.R;
import cn.songhaiqing.walle.global.Global;
import cn.songhaiqing.walle.help.CommandHelper;
import cn.songhaiqing.walle.help.FormatHelper;

public class BLEService extends Service {
    public static final String ACTION_DATA_AVAILABLE = "com.gzgamut.smart_movement.bluetooth.le.ACTION_DATA_AVAILABLE";
    public static final String ACTION_DEVICE_FOUND = "com.gzgamut.smart_movement.bluetooth.le.ACTION_DEVICE_FOUND";
    public static final String ACTION_GATT_CONNECTED = "com.gzgamut.smart_movement.bluetooth.le.ACTION_GATT_CONNECTED";
    public static final String ACTION_GATT_CONNECTED_FAIL = "com.gzgamut.smart_movement.bluetooth.le.ACTION_GATT_CONNECTED_FAIL";
    public static final String ACTION_GATT_DISCONNECTED = "com.gzgamut.smart_movement.bluetooth.le.ACTION_GATT_DISCONNECTED";
    public static final String ACTION_GATT_SERVICES_DISCOVERED = "com.gzgamut.smart_movement.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
    public static final String ACTION_READ_REMOVE_RSSI = "com.gzgamut.smart_movement.bluetooth.le.READ_REMOVE_RSSI";
    public static final String ACTION_RECEIVE_CALL_STATE_RESULST = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RECEIVE_CALL_STATE_RESULST";
    public static final String ACTION_RECEIVE_DATA = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RECEIVE_DATA";
    public static final String ACTION_RECEIVE_UPDATE_RESULST = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RECEIVE_UPDATE_RESULST";
    public static final String ACTION_RETURN_ACTIVITY = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_ACTIVITY";
    public static final String ACTION_RETURN_ACTIVITY_COUNT = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_ACTIVITY_COUNT";
    public static final String ACTION_RETURN_BATTERY = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_BATTERY";
    public static final String ACTION_RETURN_BEACON_ZOON = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_BEACON_ZOON";
    public static final String ACTION_RETURN_BOUND_RESULT = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_BOUND_INFO";
    public static final String ACTION_RETURN_CALLIN_RESULT = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_CALLIN";
    public static final String ACTION_RETURN_CLEAR = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_CLEAR";
    public static final String ACTION_RETURN_PRESS_KEY = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_PRESS_KEY";
    public static final String ACTION_RETURN_RESET = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_RESET";
    public static final String ACTION_RETURN_SENSOR_DATA = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA";
    public static final String ACTION_RETURN_SENSOR_DATA01 = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA01";
    public static final String ACTION_RETURN_SENSOR_DATA02 = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA02";
    public static final String ACTION_RETURN_SENSOR_DATA04 = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA04";
    public static final String ACTION_RETURN_SENSOR_DATA08 = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA08";
    public static final String ACTION_RETURN_SENSOR_DATA10 = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_DATA10";
    public static final String ACTION_RETURN_SENSOR_TYPE = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_SENSOR_TYPE";
    public static final String ACTION_RETURN_VERSION_RESULT = "com.gzgamut.smart_movement.bluetooth.le.ACTION_RETURN_VERSION_RESULT";
    public static final String ACTION_UPDATE_HOME_UI = "com.gzgamut.smart_movement.ACTION_UPDATE_HOME_UI";
    public static final String ACTION_WRITE_ALARM_SUCCESS = "com.gzgamut.smart_movement.bluetooth.le.ACTION_WRITE_ALARM_SUCCESS";
    public static final String ACTION_WRITE_DESCRIPTOR = "com.gzgamut.smart_movement.bluetooth.le.WRITE_DESCRIPTOR";
    public static final String KEY_RECEIVE_DATA = "com.gzgamut.smart_movement.le.UPADTEDATA";
    public static final String KEY_RSSI_VALUE = "KEY_RSSI_VALUE";
    public static final int STATE_CONNECTED = 2;
    public static final int STATE_CONNECTING = 1;
    public static final int STATE_DISCONNECTED = 0;
    private String TAG = "BLEService";
    private final IBinder mBinder = new LocalBinder();
    private BluetoothAdapter mBluetoothAdapter;
    private String mBluetoothDeviceAddress;
    private String mBluetoothDeviceName;
    private BluetoothGatt mBluetoothGatt;
    private BluetoothManager mBluetoothManager;
    private int mConnectionState = 0;
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        /* class com.gzgamut.smart_movement.service.BLEService.AnonymousClass1 */

        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == 2) {
                Log.i(BLEService.this.TAG, "Connected to GATT server.");
                BLEService.this.mConnectionState = 2;
                BLEService.this.broadcastUpdate(BLEService.ACTION_GATT_CONNECTED);
                Log.i(BLEService.this.TAG, "Attempting to start service discovery:" + BLEService.this.mBluetoothGatt.discoverServices());
            } else if (newState != 0) {
            } else {
                if (BLEService.this.mConnectionState == 2) {
                    Log.i(BLEService.this.TAG, "Disconnected from GATT server.");
                    BLEService.this.mConnectionState = 0;
                    BLEService.this.broadcastUpdate(BLEService.ACTION_GATT_DISCONNECTED);
                    BLEService.this.close();
                    return;
                }
                Log.i(BLEService.this.TAG, "Connect fail.");
                BLEService.this.mConnectionState = 0;
                BLEService.this.disconnect();
                BLEService.this.close();
                BLEService.this.broadcastUpdate(BLEService.ACTION_GATT_CONNECTED_FAIL);
            }
        }

        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i(BLEService.this.TAG, "onServicesDiscovered received: " + status);
            if (status == 0) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_GATT_SERVICES_DISCOVERED);
                return;
            }
            BLEService.this.disconnect();
            BLEService.this.close();
            Log.w(BLEService.this.TAG, "onServicesDiscovered received: " + status);
        }

        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            Log.i(BLEService.this.TAG, "onCharacteristicWrite");
            byte[] value1 = characteristic.getValue();
            BLEService.this.logMessageSend(value1);
            // ParserHelper.getByteValue(value1[0]);
            if (status == 0) {
                BLEService.this.time_send = System.currentTimeMillis();
                byte[] value = characteristic.getValue();
                int title = 1;// ParserHelper.getByteValue(value[0]);
                if (title == 1) {
                    Log.i(BLEService.this.TAG, "write date time success");
                    Log.e(BLEService.this.TAG, "write date time success value:" + Arrays.toString(value));
                } else if (title == 2) {
                    Log.i(BLEService.this.TAG, "write day mode success");
                    Log.e(BLEService.this.TAG, "write day mode success value:" + Arrays.toString(value));
                } else if (title == 3) {
                    BLEService.this.broadcastUpdate(BLEService.ACTION_WRITE_ALARM_SUCCESS);
                    System.out.println("ACTION_WRITE_ALARM_SUCCESS");
                } else if (title == 10) {
                    Log.i(BLEService.this.TAG, "write language success");
                } else if (title == 11) {
                    Log.i(BLEService.this.TAG, "write target success");
                } else if (title == 13) {
                    Log.i(BLEService.this.TAG, "write hour success");
                } else if (title == 12) {
                    Log.i(BLEService.this.TAG, "write body success");
                } else if (title == 19) {
                    Log.i(BLEService.this.TAG, "write activity success");
                } else if (title == 16) {
                    Log.i(BLEService.this.TAG, "write activity_count success");
                } else if (title == 96) {
                    Log.i(BLEService.this.TAG, "write sensor success");
                } else if (title == 97) {
                    Log.i(BLEService.this.TAG, "write sensor_value success");
                } else if (title == 15) {
                    Log.i(BLEService.this.TAG, "write lostmode success");
                }
            }
        }

        public synchronized void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            System.out.println("onCharacteristicChanged");
            long time_receive = System.currentTimeMillis();
            BLEService.this.time_Interval = time_receive - BLEService.this.time_send;
            byte[] value = characteristic.getValue();
            BLEService.this.logMessageReceive(value);
            int title = 1;// ParserHelper.getByteValue(value[0]);
            Log.i(BLEService.this.TAG, "onCharacteristicChanged:" + Arrays.toString(value));
            Log.i(BLEService.this.TAG, "" + characteristic.getUuid() + ",   " + Global.UUID_SPOTA_SERV_STATUS);
            if (characteristic.getUuid().equals(Global.UUID_SPOTA_SERV_STATUS) || characteristic.getUuid().equals(Global.UUID_SPOTA_MEM_INFO)) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_RECEIVE_UPDATE_RESULST, BLEService.KEY_RECEIVE_DATA, value);
                Log.i(BLEService.this.TAG, "广播升级状态");
            } else if (title == 224) {
                Log.i(BLEService.this.TAG, "*****广播 sensor type");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_SENSOR_TYPE, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 225) {
                Log.i(BLEService.this.TAG, "*****广播 sensor data");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_SENSOR_DATA, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 193) {
                Log.i(BLEService.this.TAG, "*****广播 zoon");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_BEACON_ZOON, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 143) {
                Log.i(BLEService.this.TAG, "*****广播 bound");
                Log.i("test", "在AboutFragment收到解绑成功返回广播，value:" + Arrays.toString(value));
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_BOUND_RESULT, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 240) {
                Log.i(BLEService.this.TAG, "*****88888888888888888888888888888888888888888888888888888广播 version");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_VERSION_RESULT, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 176) {
                Log.i(BLEService.this.TAG, "*****广播 battery");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_BATTERY, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 241) {
                Log.i(BLEService.this.TAG, "*****广播 finish sync");
            } else if (title == 160) {
                Log.i(BLEService.this.TAG, "*****广播 来电提醒");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_CALLIN_RESULT, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 216) {
                BLEService.this.receive_blood_data(value);
            } else if (title == 175) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_RECEIVE_CALL_STATE_RESULST, BLEService.KEY_RECEIVE_DATA, value);
            } else if (title == 158) {
                BLEService.this.actionReceiveCurrentHourActivity(value);
            } else if (title == 254) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_RESET);
            } else if (title == 253) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_RETURN_CLEAR);
            } else {
                Log.i(BLEService.this.TAG, "*****广播 data");
                BLEService.this.broadcastUpdate(BLEService.ACTION_RECEIVE_DATA, BLEService.KEY_RECEIVE_DATA, characteristic.getValue());
            }
        }

        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        }

        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            Log.i("test", "descriptor uuid = " + descriptor.getUuid());
            if (status == 0) {
                BLEService.this.broadcastUpdate(BLEService.ACTION_WRITE_DESCRIPTOR);
                return;
            }
            BLEService.this.disconnect();
            BLEService.this.close();
        }

        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            BLEService.this.broadcastRSSI(rssi);
        }
    };
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        /* class com.gzgamut.smart_movement.service.BLEService.AnonymousClass3 */

        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            Bundle mBundle = new Bundle();
            mBundle.putParcelable("android.bluetooth.device.extra.DEVICE", device);
            mBundle.putInt("android.bluetooth.device.extra.RSSI", rssi);
            Intent intent = new Intent();
            intent.setAction(BLEService.ACTION_DEVICE_FOUND);
            intent.putExtras(mBundle);
            BLEService.this.sendBroadcast(intent);
        }
    };
    private long time_Interval;
    private long time_send;

    public long getInterval() {
        return this.time_Interval;
    }

    public String getDeviceAddress() {
        return this.mBluetoothDeviceAddress;
    }

    public String getDeviceName() {
        return this.mBluetoothDeviceName;
    }

    public int getConnectionState() {
        return this.mConnectionState;
    }

    public void setmConnectionState(int mConnectionState2) {
        this.mConnectionState = mConnectionState2;
    }

    public class LocalBinder extends Binder {
        public LocalBinder() {
        }

        public BLEService getService() {
            return BLEService.this;
        }
    }

    public IBinder onBind(Intent intent) {
        return this.mBinder;
    }

    public boolean onUnbind(Intent intent) {
        close();
        MyApp.getIntance().mService = null;
        return super.onUnbind(intent);
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onCreate() {
        initialize();
        forceGroundStart();
    }

    public void forceGroundStart() {
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
                .setContentTitle(getString(R.string.Force_Service_Noty_Title))
                .setSmallIcon(R.mipmap.image_app_logo_smmm)
                .setWhen(System.currentTimeMillis());
        startForeground(1, builder.build());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void actionReceiveCurrentHourActivity(byte[] value) {
        int profileID = MainActivity.initProfileID(getApplicationContext());
        //DatabaseProvider.getInstance(getApplicationContext()).saveHistoryHour(getApplicationContext(), SyncResultHelper.parseCurrentHourActivity(getApplicationContext(), value), profileID, 5);
        broadcastUpdate(ACTION_UPDATE_HOME_UI);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_blood_data(byte[] value) {
        int profileID = MainActivity.initProfileID(getApplicationContext());
        Calendar calendar = Calendar.getInstance();
        if (value.length >= 3) {
            int sbp = 1;// ParserHelper.getByteValue(value[1]);
            int dbp = 1;// ParserHelper.getByteValue(value[2]);
            String date = FormatHelper.sdf_dd_MM_yyyy.format(calendar.getTime());
            //   DatabaseProvider.getInstance(getApplicationContext()).saveBloodDate(getApplicationContext(), date, profileID);
            //   DatabaseProvider.getInstance(getApplicationContext()).saveBloodData(getApplicationContext(), date, FormatHelper.sdf_HH_mm.format(calendar.getTime()), sbp + MqttTopic.TOPIC_LEVEL_SEPARATOR + dbp + "mmhg", profileID);
            EventBus.getDefault().post(new BloodEvent());
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void logMessageSend(byte[] value) {
        String[] i = new String[value.length];
        for (int j = 0; j < i.length; j++) {
            i[j] = Integer.toHexString(value[j]);
        }
        Log.i("111111", "发送数据为:" + Arrays.toString(i));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void logMessageReceive(byte[] value) {
        String[] i = new String[value.length];
        for (int j = 0; j < i.length; j++) {
            i[j] = Integer.toHexString(value[j]);
        }
        Log.i("111111", "接收到的数据为:" + Arrays.toString(i));
    }

    public void set_date_time() {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getDateTimeValue());
    }

    public void set_clear_data() {
        byte[] value = new byte[20];
        value[0] = 125;
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void set_target(Context context) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getGoalValue(context));
    }

    public void set_day_mode(Context context) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getDayModeValue(context));
    }

    public void set_alarm(Context context) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getAlarmValue(context));
    }

    public void set_alarm2(Context context) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getAlarmValue2(context));
    }

    public void set_reset() {
        byte[] value = new byte[20];
        value[0] = 126;
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void set_call_state() {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getOpenPhoneState());
    }

    public void set_profile(Context context) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getProfileValue(context));
    }

    public void send_notifi(byte[] value) {
        if (getConnectionState() == 2) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void write_camera(int type) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getCameraValue(type));
    }

    public void get_activity_count() {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getActivityCountValue());
    }

    public void get_activity_by_sn(int sn, int hour) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getActivityValue(sn, hour));
    }

    public void get_sleep_by_sn(int sn, int hour) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getSleepValue(sn, hour));
    }

    public void write_call_reminder(Context context, String number) {
        byte[] value;
        if (number != null && (value = CommandHelper.getCallValue(context, number)) != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void write_sms_reminder(String number) {
        byte[] value = CommandHelper.getSMSValue(number);
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void write_call_termination() {
        final byte[] value = CommandHelper.getCallTerminationValue();
        if (value != null) {
            if (MyApp.isHeartSetWork) {
                new Thread(new Runnable() {
                    /* class com.gzgamut.smart_movement.service.BLEService.AnonymousClass2 */

                    public void run() {
                        do {
                        } while (MyApp.isHeartSetContinue);
                        MyApp.isHeartSetContinue = true;
                        BLEService.this.writeCharacteristic(BLEService.this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
                    }
                }).start();
            } else {
                writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            }
        }
        Log.i("aaaaa", "*********** stop call");
    }

    public void get_id() {
        byte[] value = CommandHelper.getZoonValue();
        System.out.println("service:get_id");
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void send_prepareAirUp() {
        byte[] value = new byte[20];
        value[0] = Byte.MAX_VALUE;
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void set_bound_state(byte type) {
        System.out.println("Service  Bound State:" + ((int) type));
        byte[] value = CommandHelper.getBoundValue(type);
        System.out.println("Service  Bound State  value:" + Arrays.toString(value));
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void set_bound_state_no_confirm() {
        byte[] value = new byte[20];
        value[0] = 15;
        value[1] = 2;
        value[2] = 1;
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void get_bound_state() {
        byte[] value = CommandHelper.getBoundValue();
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void get_battery_level() {
        byte[] value = CommandHelper.getBatteryValue();
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void get_version_code() {
        byte[] value = CommandHelper.getVersionValue();
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }
    //震动
    public void setTouchVibration(boolean isTouchVibration, int time) {
        byte[] value = CommandHelper.getTouchVibrationvalue(isTouchVibration, time);
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            Log.i("touch vrbration", "value = " + Arrays.toString(value));
        }
    }

    public void set_unit(int type) {
        byte[] value = CommandHelper.getUnitValue(type);
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            Log.i("test", "unit value = " + Arrays.toString(value));
        }
    }

    public void set_time_format(Context context) {
        byte[] value = CommandHelper.getTimeFormatValue(context);
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            Log.i("test", "time value = " + ((int) value[0]) + "  " + ((int) value[1]));
        }
    }

    public void set_heart(byte type) {
        byte[] value = CommandHelper.getHeartValue(type);
        if (value != null) {
            writeCharacteristicDefault(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            Log.i("heart", "heart value = " + Arrays.toString(value));
        }
    }

    public void set_blood_pressure(int sbp, int dbp) {
        writeCharacteristicDefault(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getBloodPressure(sbp, dbp));
    }

    public void set_lost_mode(int type) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getLostModeValue(type));
    }

    public void finish_sync() {
        byte[] value = new byte[20];
        value[0] = 113;
        if (value != null) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
        }
    }

    public void set_gpio_map() {
        ///writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_GPIO_MAP, SkyUpdateHelper.get_gpio_map());
    }

    public void set_mem_dev() {
        // writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_MEM_DEV, SkyUpdateHelper.get_mem_dev());
    }

    public void set_mem_dev_end() {
        // writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_MEM_DEV, SkyUpdateHelper.get_mem_dev_end());
    }

    public void set_patch_len(int length) {
        // writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_PATCH_LEN, SkyUpdateHelper.get_patch_len(length));
    }

    public void set_patch_data(byte[] value) {
        writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_PATCH_DATA, value);
    }

    public void read_progress() {
        readCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_MEM_INFO);
    }

    public void set_notify_true() {
        setCharactoristicNotifyAndWriteDescriptor(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_NOTI, Global.UUID_DESCRIPTOR_CONFIGURATION);
    }

    public void set_notify_true_img() {
        setCharactoristicNotifyAndWriteDescriptor(this.mBluetoothGatt, Global.UUID_SERVICE_IMG, Global.UUID_SPOTA_SERV_STATUS, Global.UUID_DESCRIPTOR_CONFIGURATION);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void broadcastUpdate(String action) {
        sendBroadcast(new Intent(action));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void broadcastUpdate(String action, String key, byte[] value) {
        Intent intent = new Intent(action);
        intent.putExtra(key, value);
        sendBroadcast(intent);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void broadcastRSSI(int rssi) {
        Intent intent = new Intent(ACTION_READ_REMOVE_RSSI);
        intent.putExtra(KEY_RSSI_VALUE, rssi);
        sendBroadcast(intent);
    }

    public boolean initialize() {
        if (this.mBluetoothManager == null) {
            this.mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            if (this.mBluetoothManager == null) {
                Log.e(this.TAG, "Unable to initialize BluetoothManager.");
                return false;
            }
        }
        if (this.mBluetoothAdapter == null) {
            this.mBluetoothAdapter = this.mBluetoothManager.getAdapter();
            if (this.mBluetoothAdapter == null) {
                Log.e(this.TAG, "Unable to obtain a BluetoothAdapter.");
                return false;
            }
        }
        return true;
    }

    public BluetoothAdapter openBluetooth(Activity context) {
        if (this.mBluetoothAdapter == null && !initialize()) {
            return null;
        }
        if (!this.mBluetoothAdapter.isEnabled()) {
            context.startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), 11);
        }
        return this.mBluetoothAdapter;
    }

    public boolean connect(String address, boolean is) {
        if (this.mBluetoothAdapter == null || address == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }
        BluetoothDevice device = this.mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(this.TAG, "Device not found.  Unable to connect.");
            return false;
        }
        this.mBluetoothGatt = device.connectGatt(this, false, this.mGattCallback);
        Log.d(this.TAG, "Trying to create a new connection.");
        this.mBluetoothDeviceAddress = address;
        this.mBluetoothDeviceName = device.getName();
        this.mConnectionState = 1;
        return true;
    }

    public void disconnect() {
        if (this.mBluetoothAdapter == null || this.mBluetoothGatt == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else if (this.mBluetoothGatt != null) {
            this.mBluetoothGatt.disconnect();
        }
    }

    public void close() {
        if (this.mBluetoothGatt != null) {
            this.mBluetoothGatt.close();
            this.mBluetoothGatt = null;
        }
    }

    public List<BluetoothGattService> getSupportedGattServices() {
        if (this.mBluetoothGatt == null) {
            return null;
        }
        return this.mBluetoothGatt.getServices();
    }

    public boolean getRssiVal() {
        if (this.mBluetoothGatt == null) {
            return false;
        }
        System.out.println("ReadRssi");
        return this.mBluetoothGatt.readRemoteRssi();
    }

    public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (this.mBluetoothAdapter == null || this.mBluetoothGatt == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            this.mBluetoothGatt.writeCharacteristic(characteristic);
        }
    }

    public synchronized void writeCharacteristic(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_character, byte[] value) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_character);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGattCharacteristic.setValue(value);
            mBluetoothGattCharacteristic.setWriteType(1);
            mBluetoothGatt2.writeCharacteristic(mBluetoothGattCharacteristic);
        } else if (mBluetoothGatt2 == null) {
            Log.i(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.i(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    public synchronized void writeCharacteristicDefault(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_character, byte[] value) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_character);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGattCharacteristic.setValue(value);
            mBluetoothGattCharacteristic.setWriteType(2);
            mBluetoothGatt2.writeCharacteristic(mBluetoothGattCharacteristic);
        } else if (mBluetoothGatt2 == null) {
            Log.i(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.i(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    public synchronized void readCharacteristic(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_character) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_character);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGatt2.readCharacteristic(mBluetoothGattCharacteristic);
        } else if (mBluetoothGatt2 == null) {
            Log.i(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.i(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    public void writeDescriptor(BluetoothGatt mBluetoothGatt2, BluetoothGattDescriptor bluetoothGattDescriptor) {
        if (this.mBluetoothAdapter == null || mBluetoothGatt2 == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            mBluetoothGatt2.writeDescriptor(bluetoothGattDescriptor);
        }
    }

    public void readCharacteristic(BluetoothGatt mBluetoothGatt2, BluetoothGattCharacteristic characteristic) {
        if (this.mBluetoothAdapter == null || mBluetoothGatt2 == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            mBluetoothGatt2.readCharacteristic(characteristic);
        }
    }

    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
        if (this.mBluetoothAdapter == null || this.mBluetoothGatt == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            this.mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        }
    }

    public void setCharactoristicNotifyAndWriteDescriptor(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_characteristic, UUID uuid_descriptor) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_characteristic);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGatt2.setCharacteristicNotification(mBluetoothGattCharacteristic, true);
            BluetoothGattDescriptor bluetoothGattDescriptor = mBluetoothGattCharacteristic.getDescriptor(uuid_descriptor);
            if (bluetoothGattDescriptor != null) {
                bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt2.writeDescriptor(bluetoothGattDescriptor);
            }
        } else if (mBluetoothGatt2 == null) {
            Log.i(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.i(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    public void setCharactoristicNotify(UUID uuid_service, UUID uuid_characteristic) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(this.mBluetoothGatt, uuid_service), uuid_characteristic);
        if (this.mBluetoothGatt != null && mBluetoothGattCharacteristic != null) {
            this.mBluetoothGatt.setCharacteristicNotification(mBluetoothGattCharacteristic, true);
        } else if (this.mBluetoothGatt == null) {
            Log.i(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.i(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    private BluetoothGattService getBluetoothGattService(BluetoothGatt mBluetoothGatt2, UUID UUID_SERVICE) {
        if (mBluetoothGatt2 != null) {
            BluetoothGattService mBluetoothGattServer = mBluetoothGatt2.getService(UUID_SERVICE);
            if (mBluetoothGattServer != null) {
                return mBluetoothGattServer;
            }
            Log.i(this.TAG, "getBluetoothGattService, bluetoothgatt get service uuid:" + UUID_SERVICE + " is null");
        } else {
            Log.i(this.TAG, "mBluetoothGatt is null");
        }
        return null;
    }

    private BluetoothGattCharacteristic getBluetoothGattCharacteristic(BluetoothGattService mBluetoothGattService, UUID UUID_CHARACTERISTIC) {
        if (mBluetoothGattService != null) {
            BluetoothGattCharacteristic mBluetoothGattCharacteristic = mBluetoothGattService.getCharacteristic(UUID_CHARACTERISTIC);
            if (mBluetoothGattCharacteristic != null) {
                return mBluetoothGattCharacteristic;
            }
            Log.i(this.TAG, "getBluetoothGattCharacteristic, bluetoothGattServer get characteristic uuid:" + UUID_CHARACTERISTIC + " is null");
        } else {
            Log.i(this.TAG, "mBluetoothGattServer is null");
        }
        return null;
    }

    public void refreshDeviceCach() {
        if (this.mBluetoothGatt != null) {
            try {
                Method refresh = this.mBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
                if (refresh != null) {
                    ((Boolean) refresh.invoke(this.mBluetoothGatt, new Object[0])).booleanValue();
                    Log.d(this.TAG, "refreshDeviceCash");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void scan(boolean start) {
        if (this.mBluetoothAdapter == null) {
            Log.i(this.TAG, "bluetoothadapter is null");
        } else if (start) {
            refreshDeviceCach();
            this.mBluetoothAdapter.startLeScan(this.mLeScanCallback);
        } else {
            this.mBluetoothAdapter.stopLeScan(this.mLeScanCallback);
        }
    }
}
